package utn.tsb.tsbxml;

import com.trolltech.qt.core.Qt.ConnectionType;
import com.trolltech.qt.gui.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class NodeContent extends QFrame{

    private QLineEdit lineName;
    private QLineEdit lineValue;
    private QTableWidget table;
    private QPushButton btnSave;
    private QPushButton btnCancel;
    private QPushButton btnAdd;
    private QPushButton btnRemove;
    private QLabel lblValue;
    private TreeNode node;

    public NodeContent(){
        this.setObjectName("panel");
        QVBoxLayout vbox = new QVBoxLayout(this);

        QGridLayout grid = new QGridLayout();
        grid.addWidget(new QLabel("Nombre:"), 0, 0);
        this.lblValue = new QLabel("Valor:");
        grid.addWidget(this.lblValue, 1, 0);
        grid.addWidget(new QLabel("Atributos:"), 2, 0);
        this.lineName = new QLineEdit();
        this.lineValue = new QLineEdit();
        grid.addWidget(this.lineName, 0, 1);
        grid.addWidget(this.lineValue, 1, 1);
        vbox.addLayout(grid);
        this.table = new QTableWidget(1, 2);
        this.table.removeRow(0);
        ArrayList headers = new ArrayList();
        headers.add("Nombre");
        headers.add("Valor");
        table.setHorizontalHeaderLabels(headers);
        table.horizontalHeader().setStretchLastSection(true);
        table.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows);
        vbox.addWidget(this.table);
        this.btnRemove = new QPushButton("Remove");
        this.btnAdd = new QPushButton("Add");
        QHBoxLayout hboxTable = new QHBoxLayout();
        hboxTable.addWidget(this.btnRemove);
        hboxTable.addWidget(this.btnAdd);
        vbox.addLayout(hboxTable);
        QSpacerItem spacer = new QSpacerItem(1, 50);
        vbox.addSpacerItem(spacer);

        QHBoxLayout hbox = new QHBoxLayout();
        this.btnSave = new QPushButton("Guardar!");
        this.btnCancel = new QPushButton("Cancelar");
        hbox.addWidget(this.btnCancel);
        hbox.addWidget(this.btnSave);
        vbox.addLayout(hbox);

        this.btnCancel.clicked.connect(this, "hide()");
        this.btnSave.clicked.connect(this, "saveData()");
        this.btnAdd.clicked.connect(this, "addRow()");
        this.btnRemove.clicked.connect(this, "removeRow()");
    }

    private void addRow(){
        this.table.insertRow(this.table.rowCount());
    }

    private void removeRow(){
        int index = this.table.currentRow();
        if(index > -1){
            this.table.removeRow(index);
        }
    }

    private void saveData(){
        this.node.setText(0, this.lineName.text());
        this.node.setValue(this.lineValue.text());

        System.out.println(table.rowCount());

        HashMap<String, String> atts = new HashMap<String, String>();
        for(int i = 0; i < table.rowCount(); i++){
            String name = this.table.item(i, 0).text();
            if(name.trim().length() == 0){
                continue;
            }
            String value = this.table.item(i, 1).text();
            atts.put(name, value);
        }
        this.node.setAttributes(atts);
        this.hide();
    }

    public void loadData(TreeNode nodo){
        this.node = nodo;
        this.lineName.setText(nodo.text(0));
        this.lineValue.setText(nodo.getValue());
        if(nodo.childCount() > 0){
            this.lblValue.hide();
            this.lineValue.hide();
        }else{
            this.lblValue.show();
            this.lineValue.show();
        }
        this.loadTable(nodo);
    }

    private void loadTable(TreeNode nodo){
        Set<String> keys = nodo.getAttributes().keySet();
        String[] array = keys.toArray(new String[0]);
        for(int i = 0; i < table.rowCount(); i++){
            this.table.removeRow(0);
        }
        
        for(int i = 0; i < array.length; i++){
            table.insertRow(i);
            QTableWidgetItem itemName = new QTableWidgetItem(array[i]);
            String val = nodo.getAttributes().get(array[i]);
            QTableWidgetItem itemVal = new QTableWidgetItem(val);
            table.setItem(i, 0, itemName);
            table.setItem(i, 1, itemVal);
        }
    }

}
